package com.atguigu.service;

import com.atguigu.domain.*;

/**
 *      负责将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法
 *
 * @author : leimingchao
 * @date Date : 2021年09月15日 0:12
 */
public class NameListService {

    private Employee[] employees;

    public NameListService() {

        employees = new Employee[Data.EMPLOYEES.length];

        int length = Data.EMPLOYEES.length;

        for (int i = 0; i < length; i++) {

            //员工类型
            int type = Integer.decode(Data.EMPLOYEES[i][0]);
            //获取Employee的4个基本信息
            int id = Integer.decode(Data.EMPLOYEES[i][1]);
            String name = Data.EMPLOYEES[i][2];
            int age = Integer.decode(Data.EMPLOYEES[i][3]);
            double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);

            Equipment equipment;
            double bonus;
            int stock;

            switch (type) {
                case Data.EMPLOYEE:
                    employees[i] = new Employee(id,name,age,salary);
                    break;
                case Data.PROGRAMMER:
                    equipment = this.createEquipment(i);
                    employees[i] = new Programmer(id,name,age,salary,equipment);
                    break;
                case Data.DESIGNER:
                    equipment = this.createEquipment(i);
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    employees[i] = new Designer(id,name,age,salary,equipment,bonus);
                    break;
                case Data.ARCHITECT:
                    equipment = this.createEquipment(i);
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    stock = Integer.decode(Data.EMPLOYEES[i][6]);
                    employees[i] = new Architect(id,name,age,salary,equipment,bonus,stock);
                    break;
            }
        }
    }

    /**
     * 获取指定index上的员工的设备
     * @param index
     * @return
     */
    private Equipment createEquipment(int index) {

        int key = Integer.decode(Data.EQIPMENTS[index][0]);
        String modelOrName = Data.EQIPMENTS[index][1];
        switch (key) {
            case Data.PC:
                String display = Data.EQIPMENTS[index][2];
                return new PC(modelOrName,display);
            case Data.NOTEBOOK:
                double price = Double.parseDouble( Data.EQIPMENTS[index][2]);
                return new NoteBook(modelOrName,price);
            case Data.PRINTER:
                String type = Data.EQIPMENTS[index][2];
                return new Printer(modelOrName,type);
        }
        return null;
    }

    public Employee[] getAllEmployees() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException {

        int length = employees.length;

        for (int i = 0; i < length; i++) {
            if (employees[i].getId() == id) {

                return employees[i];
            }
        }

        throw new TeamException("找不到指定的员工");
    }
}
